<template>
  <a-modal
    title="编辑"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="5" :sm="24" style="padding-left:10px;padding-right:0px">
            <a-form-item label="状态">
              <a-select style="max-width:150px" v-model="status" placeholder="请选择" @select="StatusClick">
                <a-select-option :value="item.value" v-for="(item,index) in optionStatus" :key="index" >{{ item.label }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="24" style="padding-left:10px;padding-right:10px">
            <a-form-item label="类型">
              <a-select style="max-width:150px" v-model="appTypeId" placeholder="请选择" @select="TypeClick">
                <a-select-option :value="item.value" v-for="(item,index) in optionType" :key="index" >{{ item.label }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="14" :sm="24">
            <a-input-search style="max-width:450px" placeholder="请输入用户昵称关键字" @search="onSearch" >
              <a-button type="primary" slot="enterButton" icon="search">搜索</a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      class="App_code_table"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :pagination="pagination"
      :data="loadData"
      :scroll="{ y: 240}"
      showPagination="auto"
      :customRow="rowClick"
      :rowClassName="(record) => appId && String(appId) === String(record.id) ? 'tr_able' : ''"
    >
      <span slot="packageName" slot-scope="text">
        <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="name" slot-scope="text">
        <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="appTypeName" slot-scope="text">
        <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="status" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
      <span slot="createTime" slot-scope="text">
        <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
      </span>
    </s-table>
    <template slot="footer">
      <a-button key="cancel" @click="handleCancel">取消</a-button>
      <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleNext()">确定</a-button>
    </template>
  </a-modal>
</template>

<script>
import moment from 'moment'
import pick from 'lodash.pick'
import { mapActions } from 'vuex'
import { STable, Ellipsis } from '@/components'
import { intoList } from '@/api/into'
import utils from '@/utils/common'
var timeStamp = Number(new Date())

const statusMap = {
  0: {
    status: 'processing',
    text: '审核中'
  },
  1: {
    status: 'success',
    text: '审核成功'
  },
  2: {
    status: 'warning',
    text: '审核失败'
  },
  3: {
    status: 'success',
    text: '已上架'
  },
  4: {
    status: 'default',
    text: '已下架'
  }
}

export default {
  name: 'CodeModal',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      appTypeId: '1',
      optionType: [
        { value: '1', label: '软件' },
        { value: '2', label: '游戏' }
      ],
      status: '',
      optionStatus: [
        { value: '', label: '全部' },
        { value: '0', label: '审核中' },
        { value: '1', label: '审核成功' },
        { value: '2', label: '审核失败' },
        { value: '3', label: '已上架' },
        { value: '4', label: '已下架' }
      ],
      // 查询参数
      queryParam: {
        token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
        timestamp: timeStamp,
        adminId: this.$store.getters.adminId,
        keyword: this.keyword,
        appTypeId: this.appTypeId,
        status: this.status,
        startTime: '',
        endTime: ''
      },
      keyword: '',
      pagination: {
        current: 1,
        pageSize: 20
      },
      // 表头
      columns: [
        {
          title: '应用名称',
          dataIndex: 'packageName',
          scopedSlots: { customRender: 'packageName' },
          width: 150
        },
        {
          title: '应用名称',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' },
          width: 150
        },
        {
          title: '类型',
          dataIndex: 'appTypeName',
          scopedSlots: { customRender: 'appTypeName' },
          width: 150
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          width: 150
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          scopedSlots: { customRender: 'createTime' },
          width: 150
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: pagination => {
        return intoList(Object.assign(pagination, this.queryParam)).then(res => {
          return res.data
        })
      },
      rowClick: record => ({
        on: {
          click: () => {
            if (record.appId) {
              this.appId = record.id
            }
            if (record.packageName) {
              this.packageName = record.packageName
            }
          }
        }
      }),
      packageName: '',
      appId: '',
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  methods: {
    moment,
    ...mapActions(['TypeEdit']),
    code (record) {
      this.visible = true
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(record, []))
        if (String(record.appId)) {
          this.appId = record.appId
        }
        if (String(record.appTypeId)) {
          this.appTypeId = record.appTypeId
          this.queryParam.appTypeId = record.appTypeId
          this.$refs.table.refresh()
        }
      })
    },
    StatusClick (val) { // 状态
      this.queryParam.status = val
      this.$refs.table.refresh()
    },
    TypeClick (val) { // 类型
      this.queryParam.appTypeId = val
      this.$refs.table.refresh()
    },
    onSearch (val) { // 搜索
      this.queryParam.keyword = val
      this.$refs.table.refresh()
    },
    handleCancel () {
      this.visible = false
    },
    handleNext () {
      if (this.packageName) {
        var data = {
          packageName: this.packageName,
          appId: this.appId
        }
        this.$emit('ok', data)
        this.visible = false
      } else if (!this.appId || this.appId === '') {
        this.$notification['error']({
          message: '错误',
          description: '请选择',
          duration: 4
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
  .App_code_table /deep/ .ant-table-tbody tr:hover{
    cursor: pointer;
  }
  .App_code_table /deep/ .ant-table-tbody tr.tr_able{
    background-color: #00b4ed;
    color: #fff;
  }
  .App_code_table /deep/ .ant-table-tbody tr.tr_able .ant-badge-status-text{
    color: #fff;
  }
</style>
